package cn.cdu.medical.mapper;

import cn.cdu.medical.entity.Prescription;
import cn.cdu.medical.vo.PrescriptionVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface PrescriptionMapper extends BaseMapper<Prescription> {
    
    /**
     * 根据ID查询处方详情
     *
     * @param id 处方ID
     * @return 处方详情VO
     */
    PrescriptionVO selectPrescriptionById(@Param("id") String id);

    /**
     * 分页查询处方列表
     *
     * @param page      分页参数
     * @param patientId 患者ID
     * @param doctorId  医生ID
     * @param status    状态
     * @return 处方列表
     */
    Page<PrescriptionVO> selectPrescriptionPage(Page<PrescriptionVO> page,
                                              @Param("patientId") String patientId,
                                              @Param("doctorId") String doctorId,
                                              @Param("status") String status);

    /**
     * 分页查询处方列表
     * @param page 分页参数
     * @param wrapper 查询条件
     * @return 处方列表
     */
    @Select("SELECT " +
            "p.*, " +
            "u.nickname as patient_name, " +
            "d.doctor_name as doctor_name " +
            "FROM prescription p " +
            "LEFT JOIN user u ON p.patient_id = u.id AND u.del_flag = 0 " +
            "LEFT JOIN doctor d ON p.doctor_id = d.id AND d.del_flag = 0 " +
            "${ew.customSqlSegment}")
    @Results({
        @Result(id = true, column = "id", property = "id"),
        @Result(column = "medical_record_id", property = "medicalRecordId"),
        @Result(column = "patient_id", property = "patientId"),
        @Result(column = "doctor_id", property = "doctorId"),
        @Result(column = "prescription_no", property = "prescriptionNo"),
        @Result(column = "prescription_type", property = "prescriptionType"),
        @Result(column = "status", property = "status"),
        @Result(column = "instructions", property = "instructions"),
        @Result(column = "notes", property = "notes"),
        @Result(column = "validity_start_time", property = "validityStartTime"),
        @Result(column = "validity_end_time", property = "validityEndTime"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "update_time", property = "updateTime"),
        @Result(column = "create_by", property = "createBy"),
        @Result(column = "update_by", property = "updateBy"),
        @Result(column = "del_flag", property = "delFlag"),
        @Result(column = "patient_name", property = "patientName"),
        @Result(column = "doctor_name", property = "doctorName"),
        @Result(column = "id", property = "medicines",
            many = @Many(select = "cn.cdu.medical.mapper.PrescriptionDetailMapper.selectDetailsByPrescriptionId"))
    })
    IPage<PrescriptionVO> selectPrescriptionPage(Page<PrescriptionVO> page, @Param("ew") com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Prescription> wrapper);
} 